/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package inputverifier;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 *
 * @author Gebruiker
 */
public class NumberVerifier extends InputVerifier {
    
    private int max;
    
    public NumberVerifier(int max){
        super();
        this.max = max;
    }

    @Override
    public boolean verify(JComponent input) {
        if(input instanceof JTextField){
            Pattern p = Pattern.compile("[0-9]+");
            String text = ((JTextField)input).getText();
            Matcher m = p.matcher(text);
//            if(text.length() == 0){
//                JOptionPane.showMessageDialog(input.getParent(),"Gelieve een nummer in te vullen.","Error",JOptionPane.ERROR_MESSAGE);
//                return false;
//            }
            if(m.matches()){
                int number = Integer.parseInt(text);
                if(number > max){
                    JOptionPane.showMessageDialog(input.getParent(),"Gelieve een getal kleiner als "+max+" op te geven.","Error",JOptionPane.ERROR_MESSAGE);
                    return false;
                }
                return true;
            }
            else{
                JOptionPane.showMessageDialog(input.getParent(),"Gelieve enkel cijfers te gebruiken.","Error",JOptionPane.ERROR_MESSAGE);
                return false;
            }
        }
        
        throw new UnsupportedOperationException("Deze verifier is niet van toepassing op dit soort componenten.");
    }
    
}
